import rclpy
import rclpy.logging
from rclpy.node import Node
import sys
from base_interface_demo01.srv import AddInt
from rclpy.client import Client
class AddIntClientNode(Node):
    def __init__(self):
        super().__init__('addint_client_node_py')
        self.get_logger().info('创建AddInt client Node')
        self.client: Client[AddInt] =self.create_client(AddInt,'add_int')
        while not self.client.wait_for_service(1.0):
            self.get_logger().info('服务链接中')
    def connect_server(self)->bool:
        while not self.client.wait_for_service(1.0):
            self.get_logger().info('服务链接中')
        return True
    def send_request(self,num1,num2):
        req=AddInt.Request()
        req.num1=num1
        req.num2=num2
        self.future=self.client.call_async(req)    
def main():
    if len(sys.argv)!=3:
        print(sys.argv)
        rclpy.logging.get_logger('rclpy').info("请提交两个整形数据")
        return 2
    rclpy.init()
    addint_client_node=AddIntClientNode()
    # client.connect_server()
    addint_client_node.send_request(int(sys.argv[1]),int(sys.argv[2]))
    rclpy.spin_until_future_complete(addint_client_node,addint_client_node.future)
    try:
        response: AddInt.Response=addint_client_node.future.result()
        addint_client_node.get_logger().info(f"sum={response.sum}")
    except:
        addint_client_node.get_logger().error("服务响应失败")    
    rclpy.shutdown()


if __name__ == '__main__':
    main()